No qualifying bean of type 'com.querydsl.jpa.impl.JPAQueryFactory' available: expected at least 1 bean which qualifies as autowire candidate 您所在的位置:网站首页 qualifying bean of type No qualifying bean of type 'com.querydsl.jpa.impl.JPAQueryFactory' available: expected at least 1 bean which qualifies as autowire candidate

No qualifying bean of type 'com.querydsl.jpa.impl.JPAQueryFactory' available: expected at least 1 bean which qualifies as autowire candidate

2023-03-03 19:55| 来源: 网络整理| 查看: 265

JPA Test code 작성 중 아래와 같은 에러가 터졌다.

Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase.

에러로그를 찬찬히 읽어보니.. AutoConfigureTestDatabase 옵션을 바꾸라고 얘기해준다..

ANY 옵션은 임베디드 DB(H2.. 같은) 가 있어야 대체된다고 하드라.. 그동안 습관적으로 H2를 깔고 시작했기 때문에 이 간단한 옵션 조차 잊어먹고 있었다.

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

NONE으로 바꿔서 실제 지금 연동중인 DB환경에서 테스트해보기로 했다. 그러니 위의 제목과 같은 에러가 떴다. 찾아보니, @DataJpaTest를 사용하면 별도의 스프링 빈을 등록하지 않고, 엔티티들과 EntityManager 정도만 등록해서 테스트하게 된다고 하드라. 별도로 빈을 주입시켜야 될 필요가 생겼다. 그래서 testConfig를 하나 만들었다.

@TestConfiguration public class TestConfig { @PersistenceContext private EntityManager entityManager; @Bean public JPAQueryFactory jpaQueryFactory(){ return new JPAQueryFactory(entityManager); } @Bean public QuerydslOrderRepositoryImpl querydslOrderRepository(){ return new QuerydslOrderRepositoryImpl(jpaQueryFactory(), entityManager); } }

테스트 할려는 클래스에 import

@Transactional @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @DataJpaTest // Repository들을 다 IOC 등록해둠. @Import(TestConfig.class) class ItemRepositoryUnitTest { @Autowired private ItemRepository itemRepository; @Test public void saveTest() { //BDDMocikto 방식 //given Item item = new Food(); item.setName("test1"); //when Item save = itemRepository.save(item); //then Assertions.assertThat(save.getName()).isEqualTo("test1"); //왼쪽인자가 내가 기대하는 값, 오른쪽인자가 실제 값 = 둘이 동일하면 true 리턴(테스트 성공!) } } 참고

https://www.inflearn.com/questions/23063 https://velog.io/@ewan/Test-Case-%EC%97%90%EC%84%9C-JPAQueryFactory-%EC%97%90%EB%9F%AC-%EB%B0%9C%EC%83%9D



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有